package org.sufficientlysecure.htmltextview;

import android.support.annotation.NonNull;
import android.text.Editable;

import org.xml.sax.Attributes;

import java.util.Collection;
import java.util.Stack;

/**
 * Created by admin on 2017/7/25.
 */

public class ImageTagHandler implements HtmlParser.TagHandler {

    private HttpImageGetter httpImageGetter;
    /**
     * html 标签的开始下标
     */
    private Stack<Integer> startIndex;

    /**
     * html的标签的属性值 value，如:<size value='16'></size>
     * 注：value的值不能带有单位,默认就是sp
     */
    private Stack<String> propertyValue;

    public ImageTagHandler() {
    }

    public void setHttpImageGetter(HttpImageGetter httpImageGetter) {
        this.httpImageGetter = httpImageGetter;
    }

    public HttpImageGetter getHttpImageGetter() {
        return httpImageGetter;
    }

    @Override
    public boolean handleTag(boolean opening, String tag, Editable output, Attributes attributes) {
        if (opening) {
            if (tag.equalsIgnoreCase("img")) {
                handlerStartImg(output, attributes);
                //return true;
            }
        } else {
            if (tag.equalsIgnoreCase("img")) {
                handlerEndImg(output);
                //return true;
            }
        }
        return handlerBYDefault(tag);
    }

    private void handlerStartTag(String tag, Editable output, Attributes attributes) {
        if (tag.equalsIgnoreCase("img")) {
            handlerStartImg(output, attributes);
        }
    }


    private void handlerEndTag(String tag, Editable output, Attributes attributes) {
        if (tag.equalsIgnoreCase("img")) {
            handlerEndImg(output);
        }
    }

    private void handlerStartImg(Editable output, Attributes attributes) {
        if (startIndex == null) {
            startIndex = new Stack<>();
        }
        startIndex.push(output.length());

        if (propertyValue == null) {
            propertyValue = new Stack<>();
        }

        //propertyValue.push(HtmlParser.getValue(attributes, "width"));
        //propertyValue.push(HtmlParser.getValue(attributes, "height"));
        startImg_(output, attributes, httpImageGetter);

    }

    private void handlerEndImg(Editable output) {
//        if (!isEmpty(propertyValue)) {
//            try {
//                int value = Integer.parseInt(propertyValue.pop());
//                output.setSpan(new AbsoluteSizeSpan(value), startIndex.pop(), output.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
    }

    /**
     * 参考 {@link # android.tex.Html.startImg}
     *
     * @param text
     * @param attributes
     * @param img
     */
    private void startImg_(Editable text, Attributes attributes, @NonNull HttpImageGetter img) {
        String src = attributes.getValue("", "src");
        String width = attributes.getValue("", "width");
        String height = attributes.getValue("", "height");

        img.getDrawable(src);//调用getDrawable(),设置isFirst

        int len = text.length();
        if (img.isFirst(src)) {//如果不控制的话，系统也会添加一个obj
            text.append("\uFFFC");//obj
        }

        img.loadDrawable(src, width, height);
        img.imgTagPosition(src, len, text.length());

    }

    /**
     * 集合是否为空
     *
     * @param collection
     * @return
     */
    public static boolean isEmpty(Collection collection) {
        return collection == null || collection.isEmpty();
    }

    /**
     * 返回true表示不交给系统后续处理
     * false表示交给系统后续处理
     *
     * @param tag
     * @return
     */
    private boolean handlerBYDefault(String tag) {
        if (tag.equalsIgnoreCase("del")) {
            return true;
        }
        return false;
    }
}